package com.ctsi.ssdc.service.impl;


import com.ctsi.ssdc.domain.UserVo;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallUser;
import com.ctsi.ssdc.domain.LitemallUserExample;
import com.ctsi.ssdc.service.LitemallUserService;
import com.ctsi.ssdc.repository.LitemallUserRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallUser.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallUserServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallUserRepository, LitemallUser, Integer, LitemallUserExample> 
	implements LitemallUserService {

    @Autowired
    LitemallUserRepository litemallUserRepository;


    /**
    * GET  /litemallUsers : get the litemallUsers firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallUser> findFirstStringColumn(String username,Pageable pageable){
        String str = username;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallUserExample litemallUserExample = new LitemallUserExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallUserExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallUserExample.createCriteria();
        } else{
            litemallUserExample.createCriteria().andUsernameLike("%" + str +"%");
        }
        List<LitemallUser>  data = litemallUserRepository.selectByExample(litemallUserExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallUserRepository.countByExample(litemallUserExample);
        }
        return new PageResult<LitemallUser>(data,count,count);
    }

    @Override
    public List<LitemallUser> queryByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return litemallUserRepository.selectByExample(example);
    }

    @Override
    public int updateById(LitemallUser user) {
        user.setUpdateTime(ZonedDateTime.now());
        return litemallUserRepository.updateByPrimaryKeySelective(user);
    }

    @Override
    public LitemallUser queryByOid(String openId) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
        return litemallUserRepository.selectOneByExample(example);
    }

    @Override
    public void add(LitemallUser user) {
        user.setAddTime(ZonedDateTime.now());
        user.setUpdateTime(ZonedDateTime.now());
        litemallUserRepository.insertSelective(user);
    }

    @Override
    public List<LitemallUser> queryByMobile(String mobile) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
        return litemallUserRepository.selectByExample(example);
    }

    @Override
    public List<LitemallUser> queryByOpenid(String openId) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
        return litemallUserRepository.selectByExample(example);
    }

    @Override
    public LitemallUser findById(Integer userId) {
        return litemallUserRepository.selectByPrimaryKey(userId);
    }

    @Override
    public UserVo findUserVoById(Integer creatorUserId) {
        LitemallUser user = findById(creatorUserId);
        UserVo userVo = new UserVo();
        userVo.setNickname(user.getNickname());
        userVo.setAvatar(user.getAvatar());
        return userVo;
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
                    .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
            }
        }
        return null;
    }
}
